import UniApp from '@/utils/uni'
import { ref, onMounted, watch } from 'vue'

type Handler = (data: string) => void

export const useSocketCreator = (uni: any, handler?: Handler) => {
	const app = new UniApp(uni)
	const url = ref<string>(UniApp.url)
	const setupApp = () => {
		app.get('url', (res: any) => {
			url.value = res
		})
	}
	const setUrl = (value: string) => {
		app.set('url', value, () => {
			url.value = value
			console.log('set url', value)
		})
	}
	watch(url, (_url: string) => {
		handler && handler(_url)
	}, { immediate: true })
	onMounted(setupApp)
	return setUrl
}